//
//  ImageViewController.m
//  Mooker
//
//  Created by hua zhang on 3/12/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "ImageViewController.h"
#import "DetailImageView.h"
#import "HelpImageView.h"
#import "MKNavigationController.h"
#import "AppDelegate.h"

@interface ImageViewController () <DetailImageViewDelegate,HelpImageViewDelegate>
{
    BOOL _isSupportHorizon;
}

@property (nonatomic, retain) DetailImageView *detailImageView;

@end

@implementation ImageViewController
@synthesize detailImageView = _detailImageView;

- (void)dealloc {
    _detailImageView.delegate = nil;
    RELEASE_SAFELY(_detailImageView);
}

- (id)init {
    self = [super init];
    if (self) {
        _isSupportHorizon = YES;
    }
    return self;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _isSupportHorizon = YES;
    if (![[self.view.subviews lastObject] isKindOfClass:[HelpImageView class]])
    {
    }
    self.navigationController.navigationBarHidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    if (!_isSupportHorizon || [[self.view.subviews lastObject] isKindOfClass:[HelpImageView class]]) {
        return UIInterfaceOrientationIsPortrait(interfaceOrientation);
    }else{
        return YES;
    }
}

- (void)setContentWithSources:(NSArray *)sources picture:(PicModel *)picture fromSubject:(BOOL)fromSubject
{
    [self.detailImageView setContentWithSources:sources picture:picture fromSubject:fromSubject];
}

- (DetailImageView *)detailImageView {
    if (!_detailImageView) {
        _detailImageView = [[DetailImageView alloc] initWithFrame:self.view.bounds];
        _detailImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
        _detailImageView.clipsToBounds = YES;
        _detailImageView.delegate = self;
        _detailImageView.viewController = self;
        [self.view addSubview:self.detailImageView];
    }
    return _detailImageView;
}

- (void)supportHorizon:(BOOL)isSupport{
    _isSupportHorizon = isSupport;
    
    if (!isSupport) {
        [UIApplication sharedApplication].statusBarOrientation = UIInterfaceOrientationPortrait;
        self.navigationController.view.transform = CGAffineTransformIdentity;
        CGSize size = self.navigationController.view.frame.size;
        if (size.width < size.height)
        {
            self.navigationController.view.frame = CGRectMake(0, 0, size.width, size.height);  
        }
        else
        {
            self.navigationController.view.frame = CGRectMake(0, 0, size.height, size.width);  
        }
    }
}

#pragma mark - DetalImageViewDelegate
- (void)closeDetailImageView:(DetailImageView *)detailImageView {
    if (_detailImageView == detailImageView)
    {
//        if ([DataEnvironment sharedDataEnvironment].collectionNav != nil)
//        {
//            [DataEnvironment sharedDataEnvironment].collectionNav.shouldRotate = NO;
//        }
//        else
//        {
//            [DataEnvironment sharedDataEnvironment].navController.shouldRotate = NO;
//        }

        [self supportHorizon:NO];
        [UIApplication sharedApplication].statusBarHidden = NO;
        [self.navigationController popViewControllerWithNavigationControllerTransition];
    }
}

- (void)clearData
{
    [self closeDetailImageView:self.detailImageView];
}

- (void)willEnterOtherView:(DetailImageView *)detailImageView
{
//    if ([DataEnvironment sharedDataEnvironment].collectionNav != nil)
//    {
//        [DataEnvironment sharedDataEnvironment].collectionNav.shouldRotate = NO;
//    }
//    else
//    {
//        [DataEnvironment sharedDataEnvironment].navController.shouldRotate = NO;
//    }
    
    [self supportHorizon:NO];
}
- (void)detailImageView:(DetailImageView *)detailImageView forwardPicture:(UIImage*)image singlePic:(SinglePic *)singlePic{
    [self supportHorizon:NO];
    
}
#pragma mark - HelpImageViewDelegate
- (void)helpImageViewDidClosed
{
//    if ([DataEnvironment sharedDataEnvironment].collectionNav != nil)
//    {
//        [DataEnvironment sharedDataEnvironment].collectionNav.shouldRotate = YES;
//    }
//    else
//    {
//        [DataEnvironment sharedDataEnvironment].navController.shouldRotate = YES;
//    }
}

@end
